//
//  Commend.m
//  doumi
//
//  Created by 耿用强 on 14-6-10.
//  Copyright (c) 2014年 耿用强. All rights reserved.
//

#import "Commend.h"
#import "MBProgressHUD.h"
@class AppDelegate;
@implementation Commend
#pragma mark - HUD
+(void)showAlert:(NSString*)alertString {
    dispatch_async(dispatch_get_main_queue(), ^{
        
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.labelFont=[UIFont systemFontOfSize:14];
        hud.labelText = alertString;
        hud.labelColor=[UIColor whiteColor];
        hud.margin = 15.f;
        hud.cornerRadius=8.f;
        //        hud.yOffset = 150.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1.5];
    });
    
}

+(NSString*)getNowMonth
{
    NSDate *nowTime = [NSDate date];
    NSDateFormatter *fmtter =[[NSDateFormatter alloc] init];
    [fmtter setDateFormat:@"yyyy-MM"];
    return [fmtter stringFromDate:nowTime] ;
}
+(NSString*)getNowDate
{
    NSDate *nowTime = [NSDate date];
    NSDateFormatter *fmtter =[[NSDateFormatter alloc] init];
    [fmtter setDateFormat:@"yyyy-MM-dd"];
    return [fmtter stringFromDate:nowTime] ;
}
+(NSString*)getTimeStr:(NSString*)dateString
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    // this is imporant - we set our input date format to match our input string
    // if format doesn't match you'll get nil from your string, so be careful
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *dateFromString = [[NSDate alloc] init];
    // voila!
    dateFromString = [dateFormatter dateFromString:dateString];
    
    NSString*str=  [dateFormatter stringFromDate:dateFromString];
    return str;
}
+(NSString*)getSystemTime
{
    NSDate *nowTime = [NSDate date];
    NSDateFormatter *fmtter =[[NSDateFormatter alloc] init];
    [fmtter setDateFormat:@"yyyyMMddHHmmss"];
    return [fmtter stringFromDate:nowTime] ;
}

+(void)writeLoginPlist:(NSDictionary*)aDic string:(NSString*)str//通用存储plist文件
{
    NSDictionary * array = [[NSDictionary alloc] initWithDictionary:aDic];
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths    objectAtIndex:0];
    NSString *filename=[path stringByAppendingPathComponent:str];
    
    NSFileManager* fm = [NSFileManager defaultManager];
//    NSLog(@"===%@",filename);
    [fm createFileAtPath:filename contents:nil attributes:nil];
    [array writeToFile:filename  atomically:YES];
}
+ (NSDictionary *)getLoginPlist:(NSString *)str
{
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths    objectAtIndex:0];
    NSString *filename=[path stringByAppendingPathComponent:str];
    
    NSDictionary * dic=[[NSDictionary alloc] initWithContentsOfFile:filename];
    return dic;
}

+(void)deleteLoginPlist:(NSString*)str
{
    NSFileManager* fm = [NSFileManager defaultManager];
    
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths objectAtIndex:0];
    NSString *filename=[path stringByAppendingPathComponent:str];
    [fm removeItemAtPath:filename error:nil];
}



#pragma mark - 时间戳
+(NSString*)getNowTime
{
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[[NSDate date] timeIntervalSince1970]];
    return timeSp;
}


+(void) showTip:(NSString*)msg
{
    
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示"
                                                   message:msg
                                                  delegate:self
                                         cancelButtonTitle:@"确定"
                                         otherButtonTitles:nil];
    
    [alert show];
}


+ (NSString*)time:(NSString*)str
{
    @try {
        NSString *timestr = [NSString stringWithFormat:@"%@",str];
        
        //处理系统时间
        if ((str == nil)||(str.length <= 0)) {
            str = @"2014-08-15 22:54:31";
        }
        NSString *yearStr1 = [str substringToIndex:4];
        NSString *str4 = [str substringFromIndex:5];
        //MM-dd
        NSString *tadayStr1 = [str4 substringToIndex:5];
        NSString *M_Str = [tadayStr1 substringToIndex:2];
        NSString *d_Str = [tadayStr1 substringFromIndex:3];
        NSString *str5 = [str4 substringFromIndex:6];
        //hh-mm
        NSString *timeStr1 = [str5 substringToIndex:5];
        
        //处理当前时间
        NSDate *date = [NSDate date];
        
        NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];//实例化一个NSDateFormatter对象
        [dateFormat setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
        int seconds =abs([date timeIntervalSinceDate:[dateFormat dateFromString:timestr]]) ;
        NSString *str1 = [dateFormat stringFromDate:date];
        NSString *yearStr = [str1 substringToIndex:4];
        if (![yearStr1 isEqualToString:yearStr]) {
            yearStr = yearStr1;
            return [NSString stringWithFormat:@"%@年%@月%@日",yearStr,M_Str,d_Str];
        }else{
            if (seconds < 60) {
                return [NSString stringWithFormat:@"刚刚"];
            }
            if (60 <= seconds && seconds < 60*60 ) {
                return [NSString stringWithFormat:@"%d分钟前",seconds/60];
            }
            if (60*60 <= seconds && seconds < 24*60*60) {
                return [NSString stringWithFormat:@"%d小时前",seconds/(60*60)];
            }
            if (seconds>=24*60*60&&seconds<24*60*60*2) {
                return [NSString stringWithFormat:@"昨天"];
            }
            if (seconds>=24*60*60*2&&seconds<24*60*60*3) {
                return [NSString stringWithFormat:@"前天"];
            }
            if (seconds>=24*60*60*3&&seconds<24*60*60*4) {
                return [NSString stringWithFormat:@"3天前"];
            }
            if (seconds>=24*60*60*4&&seconds<24*60*60*5) {
                return [NSString stringWithFormat:@"4天前"];
            }
            if (seconds>=24*60*60*5&&seconds<24*60*60*6) {
                return [NSString stringWithFormat:@"5天前"];
            }
            
            if (seconds >= 24*60*60*6) {
                return [NSString stringWithFormat:@"%@月%@日",M_Str,d_Str];
            }
        }
        return @"";
    }
    @catch (NSException *exception) {
        return @"";
    }
    @finally {
        
    }
}



@end
